package com.winderinfo.yikaotianxia.ui.register;

import android.content.res.ColorStateList;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.winderinfo.yikaotianxia.R;
import com.winderinfo.yikaotianxia.core.api.Api;
import com.winderinfo.yikaotianxia.core.base.BaseActivity;
import com.winderinfo.yikaotianxia.core.mvp.Contract;
import com.winderinfo.yikaotianxia.core.mvp.Presenter;
import com.winderinfo.yikaotianxia.core.utils.ToastUtil;
import com.winderinfo.yikaotianxia.ui.login.LoginActivity;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RegisterActivity extends BaseActivity implements Contract.View {
    @BindView(R.id.reg_phone)
    EditText regPhone;
    @BindView(R.id.reg_code)
    EditText regCode;
    @BindView(R.id.reg_address)
    EditText regAddress;
    @BindView(R.id.reg_rec)
    EditText regRec;
    @BindView(R.id.reg_pwd)
    EditText regPwd;
    @BindView(R.id.reg_getcode)
    TextView regGetcode;
    @BindView(R.id.reg_reg)
    Button regReg;
    private Presenter presenter;
    private String phone;
    private String code;
    private String address;
    private String rec;
    private String pwd;
    private int time = 60;
    private boolean flag = true;
    //获取到的短信
    private String getCode = "";
    //创建Map
    Map<String, Object> map = new HashMap<>();

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    flag = false;
                    time--;
                    //regGetcode.setTextColor(0xFB7123);
                    regGetcode.setText(time + "  S");
                    if (time > 0) {
                        Message message = handler.obtainMessage(1);
                        handler.sendMessageDelayed(message, 1000);
                    } else {
                        time = 60;
                        flag = true;
                        regGetcode.setText("获取验证码");
                    }
                    break;
            }
            super.handleMessage(msg);
        }
    };

    @Override
    protected int initLayoutId() {
        return R.layout.activity_register;
    }

    @Override
    protected void initView() {
        //实例P
        presenter = new Presenter(this);
    }

    @Override
    protected void initData() {


    }


    @OnClick({R.id.reg_getcode, R.id.reg_getaddress, R.id.reg_reg})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.reg_getcode://获取验证码
                //获取输入框信息
                phone = regPhone.getText().toString();
                //判断手机号是否为空
                if (TextUtils.isEmpty(phone)) {
                    ToastUtil.showSuccess(this, "请输入手机号");
                    return;
                }
                //搬到是否可以点击
                if (flag) {
                    Message message = handler.obtainMessage(1);
                    handler.sendMessageDelayed(message, 1000);
                    //发送短信请求
                    /**
                     * mobile  手机号
                     * */
                    map.clear();
                    map.put("mobile", phone);
                    ToastUtil.showSuccess(this, "请输入手机号" + phone);
                    presenter.onPostRequest(Api.DUANXIN, map, DuanxinBean.class);
                }
                break;
            case R.id.reg_getaddress:

                break;
            case R.id.reg_reg:
                //获取输入框信息
                phone = regPhone.getText().toString();
                code = regCode.getText().toString();
                address = regAddress.getText().toString();
                rec = regRec.getText().toString();
                pwd = regPwd.getText().toString();
                //判断空
                if (TextUtils.isEmpty(phone) || TextUtils.isEmpty(code) || TextUtils.isEmpty(address) || TextUtils.isEmpty(pwd)) {
                    ToastUtil.showSuccess(this, "信息不能为空");
                    return;
                }
                //判断短信
                if (!code.equals(getCode)) {
                    ToastUtil.showWarning(this, "验证码错误");
                    return;
                }
                //请求注册
                /**
                 *@Phone 手机号
                 *@Password 密码
                 *@Area 地域 （河南省）
                 *@Recommendedcode 推荐码
                 * */
                map.clear();
                map.put("Phone", phone);
                map.put("Password", pwd);
                map.put("Area", address);
                map.put("Recommendedcode", rec);
                presenter.onPostRequest(Api.REGISTER, map, RegisterBean.class);
                break;
        }
    }

    //
    @Override
    public void onSuccess(Object data) {
        //短信
        if (data instanceof DuanxinBean) {
            this.getCode = ((DuanxinBean) data).getMsg();
        }
        //注册
        if (data instanceof RegisterBean) {
            //注册失败
            if (((RegisterBean) data).getCode() == 500) {
                ToastUtil.showSuccess(this, ((RegisterBean) data).getMsg());
            }
            //注册成功
            if (((RegisterBean) data).getCode() == 0) {
                ToastUtil.showSuccess(this, ((RegisterBean) data).getMsg());
                openActivityAndCloseThis(LoginActivity.class);
            }
        }

    }

    @Override
    public void onFailure(Object error) {

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.onUntying();
    }
}
